﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Element : MonoBehaviour
{
    public Vector2 center;
    public List<List<int>> lists;
    public List<List<Cell>> cellList;

    public void Init()
    {
        center = new Vector2(5, ShuaiManager.Instance.row - 2);

        cellList = new List<List<Cell>>
        {
            new List<Cell> { null, null, null },
            new List<Cell> { null, null, null },
            new List<Cell> { null, null, null },
        };

        int index = 0; //Random.Range(0, CellFunctions.lists.Count);
        lists = CellFunctions.lists[index];

        
        for (var y = 0; y < lists.Count; y++)
        {
            var list = lists[0];
            for (var x = 0; x < list.Count; x++)
            {
                if (lists[y][x] != 0)
                {
                    int id = Random.Range(1, ShuaiManager.Instance.type_count + 1);
                    Vector2 newP = new Vector2(center.x - 1 + x , center.y - 1 + y);

                    Debug.Log(newP);
                    Cell cell = CellFunctions.CreateCell(id, (int)newP.x, (int)newP.y, ShuaiManager.Instance.cellList);
                    cellList[y][x] = cell;
                }
            }
        }
    }

    public void Move(Vector2 _direction)
    {
        center = center + _direction;

        int startIndex_x = 0;
        int endIndex_x = 2;
        int add_x = 1;

        if (_direction == Vector2.left)
        {
            startIndex_x = 0;
            endIndex_x = 2;
            add_x = +1;

            //if ((cellList[1][1] && cellList[1][1].x <= 0) || (cellList[0][1] && cellList[0][1].x <= 0))
            //{
            //    return;
            //}
        }
        else if (_direction == Vector2.right)
        {
            startIndex_x = 2;
            endIndex_x = 0;
            add_x = -1;

            //if ((cellList[1][1] && cellList[1][1].x >= ShuaiManager.Instance.col - 1) || (cellList[2][1] && cellList[2][1].x >= ShuaiManager.Instance.col - 1))
            //{
            //    return;
            //}
        }

        for (int x = startIndex_x; _direction == Vector2.right ? x >= endIndex_x : x <= endIndex_x; x = x + add_x)
        {
            for (int y = 0; y <= 3 - 1; y++)
            {
                Cell cell = cellList[y][x];
                if (cell != null)
                {
                    ShuaiManager.Instance.cellList[cell.x][cell.y] = null;

                    cell.x += (int)_direction.x;
                    cell.y += (int)_direction.y;

                    cell.Refresh();
                    cell.RefreshPos();

                    ShuaiManager.Instance.cellList[cell.x][cell.y] = cell;
                }
            }
        }
    }

    public List<List<int>> rotate(List<List<int>> matrix)
    {
        //逆时针旋转 90 度
        //列 = 行
        //行 = n - 1 - 列(j);  n表示总行数

        List<List<int>> temp = new List<List<int>>
        {
            new List<int> {0,0,0},
            new List<int> {0,0,0},
            new List<int> {0,0,0},
        };

        var len = matrix.Count;

        for (var i = 0; i < len; i++)
        {
            for (var j = 0; j < len; j++)
            {
                var k = len - 1 - j;
                if (temp[k] == null)
                {
                    temp[k] = new List<int>();
                }
                temp[k][i] = matrix[i][j];
            }
        }

        return temp;
    }

    public List<List<Cell>> rotate3(List<List<Cell>> matrix)
    {
        //逆时针旋转 90 度
        //列 = 行
        //行 = n - 1 - 列(j);  n表示总行数

        List<List<Cell>> temp = new List<List<Cell>>
        {
            new List<Cell> { null, null, null },
            new List<Cell> { null, null, null },
            new List<Cell> { null, null, null },
        };

        var len = matrix.Count;

        for (var i = 0; i < len; i++)
        {
            for (var j = 0; j < len; j++)
            {
                var k = len - 1 - j;
                if (temp[k] == null)
                {
                    temp[k] = new List<Cell>();
                }
                temp[k][i] = matrix[i][j];
            }
        }

        return temp;
    }

    public List<List<int>> rotate2(List<List<int>> matrix)
    {
        //逆时针旋转 270 度
        //列 = 行
        //行 = n - 1 - 列(j);  n表示总行数

        List<List<int>> temp = new List<List<int>>
        {
            new List<int> {0,0,0},
            new List<int> {0,0,0},
            new List<int> {0,0,0},
        };

        var len = matrix.Count;

        for (var i = 0; i < len; i++)
        {
            for (var j = 0; j < len; j++)
            {
                var k = len - 1 - i;
                if (temp[k] == null)
                {
                    temp[k] = new List<int>();
                }
                temp[j][k] = matrix[i][j];
            }
        }

        return temp;
    }

    //    var rotate270 = function(matrix) {
    //     //逆时针旋转 270 度
    //    //行 = 列
    //    //列 = n - 1 - 行(i);  n表示总列数
    //    var temp = [];
    //    var len = matrix.length;
    //    for(var i = 0; i<len; i++){
    //        for(var j = 0; j<len; j++){
    //            var k = len - 1 - i;
    //            if(!temp[j]){
    //                temp[j] = [];
    //            }
    //temp[j][k] = matrix[i][j];
    //        }
    //    }
 
    //   return temp;
    //};

    public Cell GetACell()
    {
        for (var y = 0; y < lists.Count; y++)
        {
            var list = lists[0];
            for (var x = 0; x < list.Count; x++)
            {
                if (cellList[y][x] != null)
                {
                    Cell cell = cellList[y][x];
                    cellList[y][x] = null;
                    return cell;
                }
            }
        }

        return null;
    }

    public void Change()
    {
        lists = rotate(lists);

        List<List<Cell>> cellList_temp = new List<List<Cell>>
        {
            new List<Cell> { null, null, null },
            new List<Cell> { null, null, null },
            new List<Cell> { null, null, null },
        };

        for (var y = 0; y < lists.Count; y++)
        {
            for (var x = 0; x < lists[0].Count; x++)
            {
                if (lists[y][x] != 0)
                {
                    Vector2 newP = new Vector2(center.x - 1 + x, center.y - 1 + y);

                    Cell cell = GetACell();
                    cell.x = (int)newP.x;
                    cell.y = (int)newP.y;
                    cellList_temp[y][x] = cell;
                    cell.Refresh();

                    ShuaiManager.Instance.cellList[cell.x][cell.y] = null;
                }
            }
        }

        //ShuaiManager.Instance.cellList[cell.x][cell.y] = null;

        //cell.x += (int)_direction.x;
        //cell.y += (int)_direction.y;

        //cell.Refresh();
        //cell.RefreshPos();

        //ShuaiManager.Instance.cellList[cell.x][cell.y] = cell;

        for (var y = 0; y < lists.Count; y++)
        {
            for (var x = 0; x < lists[0].Count; x++)
            {
                if (cellList_temp[y][x] != null)
                {
                    Cell cell = cellList_temp[y][x];
                    cellList[y][x] = cell;

                    ShuaiManager.Instance.cellList[cell.x][cell.y] = cell;
                }
            }
        }
    }

    //public void Change()
    //{
    //    List<Cell> topIdList = new List<Cell>();
    //    List<int> copyIdList = new List<int>();

    //    for (int x = 0; x <= this.cellList.Count - 1; x++)
    //    {
    //        Cell cell = this.cellList[x];
    //        topIdList.Add(cell);
    //    }
    //    topIdList.ForEach(item => copyIdList.Add(item.id));

        
    //    for (int x = 0; x <= topIdList.Count - 1; x++)
    //    {
    //        Cell cell = topIdList[x];
    //        int nextIndex = x;
            
    //        nextIndex = x + 1;
    //        if (nextIndex > topIdList.Count - 1)
    //        {
    //            nextIndex = 0;
    //        }
            
    //        cell.id = copyIdList[nextIndex];
    //        cell.Refresh();
    //    }
    //}
}
